package xyz.renyumeng.xkm.es.service.impl;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import xyz.renyumeng.xkm.client.shop.ProductInfoFeignClient;
import xyz.renyumeng.xkm.dto.base.SearchPage;
import xyz.renyumeng.xkm.es.repository.EsProductRepository;
import xyz.renyumeng.xkm.es.service.EsShopService;
import xyz.renyumeng.xkm.model.es.EsProduct;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author renyumeng
 * @date Created in 20:14 2023/7/3
 */
@Slf4j
@Service
public class EsShopServiceImpl implements EsShopService {
    @Resource
    private ProductInfoFeignClient productInfoFeignClient;
    @Resource
    private EsProductRepository esProductRepository;

    @Override
    public SearchPage<List<EsProduct>> searchProduct(String key, int page, int size) {
        // 查询未下架且未售出的商品
        Page<EsProduct> searchPage = esProductRepository
                .findEsProductByDescriptiveContentAndIsSoledAndIsListed(key, 0, 0, PageRequest.of(page - 1, size));
        int totalPages = searchPage.getTotalPages();

        SearchPage<List<EsProduct>> listSearchPage = new SearchPage<>();
        listSearchPage
                .setTotal(totalPages)
                .setHasNext(searchPage.hasNext())
                .setCurrent(searchPage.getNumber() + 1)
                .setData(searchPage.getContent());
        return listSearchPage;
    }

    public int importAll() {
        List<EsProduct> allEsProduct = productInfoFeignClient.getAll();
        Iterable<EsProduct> esProductIterable = esProductRepository.saveAll(allEsProduct);
        int result = 0;
        for (EsProduct esProduct : esProductIterable) {
            result++;
        }
        return result;
    }

    @Override
    public EsProduct save(EsProduct esProduct) {
        return esProductRepository.save(esProduct);
    }

    @Override
    public EsProduct getEsProductById(Long productId) {
        AtomicReference<EsProduct> esProductAtomicReference = new AtomicReference<>();
        esProductRepository.findById(productId).ifPresent(esProductAtomicReference::set);
        return esProductAtomicReference.get();
    }

    @Override
    public SearchPage<List<EsProduct>> pageByOwnerId(Integer page, Integer size, Long ownerId) {
        Page<EsProduct> searchPage = esProductRepository
                .findEsProductByOwnerId(ownerId, PageRequest.of(page - 1, size));
        int totalPages = searchPage.getTotalPages();
        SearchPage<List<EsProduct>> listSearchPage = new SearchPage<>();
        listSearchPage
                .setTotal(totalPages)
                .setHasNext(searchPage.hasNext())
                .setCurrent(searchPage.getNumber() + 1)
                .setData(searchPage.getContent());
        return listSearchPage;
    }

    @Override
    public SearchPage<List<EsProduct>> pageByOwnerIdIsSole(Integer page, Integer size, Long ownerId, Integer isSole) {
        Page<EsProduct> searchPage = esProductRepository.findEsProductByOwnerIdAndIsSoled(ownerId, isSole, PageRequest.of(page - 1, size));
        int totalPages = searchPage.getTotalPages();
        SearchPage<List<EsProduct>> listSearchPage = new SearchPage<>();
        listSearchPage
                .setTotal(totalPages)
                .setHasNext(searchPage.hasNext())
                .setCurrent(searchPage.getNumber() + 1)
                .setData(searchPage.getContent());
        return listSearchPage;
    }

    @Override
    public Boolean getEsProductIdWithIsListed(Long productId, Integer isListed) {
        EsProduct esProduct = esProductRepository.findEsProductByIdAndIsListedAndIsSoled(productId, isListed,0);
        boolean empty = ObjectUtil.isEmpty(esProduct);
        return BooleanUtil.isTrue(empty);
    }
}
